package com.ss.android.common.util;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.view.View;
import android.view.Window;

import com.bytedance.common.utility.UIUtils;
import com.bytedance.frameworks.b.a.ComponentsManifest;
import com.ss.android.article.browser.R;
import com.ss.android.common.app.AbsApplication;

public final class ImmersedStatusBarHelper {
    public static boolean mIsEnable = false;
    public Activity mActivity;
    public View b;
    public boolean mFitsSystemWindows;
    public boolean d;
    private int f;
    private int mStatusBarColor;
    private boolean mIsUserLightStatusBar;
    private boolean mIsNightMode;
    private boolean mIsAutoSwitchStatusBarStyle;
    private ObjectAnimator mStatusBarAnimator;
    public ImmersedStatusBarHelper(Activity activity, ImmersedStatusBarConfig immersedStatusBarConfig) {
        this.mIsNightMode = com.ss.android.j.b.isNightModeToggled();
        this.mActivity = activity;
        this.mStatusBarColor = immersedStatusBarConfig.mStatusBarColor;
        this.mFitsSystemWindows = immersedStatusBarConfig.mFitsSystemWindows;
        this.d = immersedStatusBarConfig.c;
        this.mIsUserLightStatusBar = immersedStatusBarConfig.mIsUseLightStatusBar;
        this.mIsAutoSwitchStatusBarStyle = immersedStatusBarConfig.mIsAutoSwitchStatusBarStyle;
        if (immersedStatusBarConfig.f) {
            IStatusBarConfig statusBarConfig = ComponentsManifest.findComponent(IStatusBarConfig.class);
            mIsEnable = statusBarConfig != null && statusBarConfig.isEnableImmersedStatusBar();
        }
    }

    public static void setUseLightStatusBar(Window window, boolean useSystemStatusBarDarkMode) {
        if (Build.VERSION.SDK_INT >= 23 && (mIsEnable)) {
            int uiVisibility = window.getDecorView().getSystemUiVisibility();
            View v1 = window.getDecorView();
            if (useSystemStatusBarDarkMode) {
                uiVisibility |= 8192;
            } else {
                uiVisibility &= -8193;
            }

            v1.setSystemUiVisibility(uiVisibility);
            if (!DeviceUtils.isMiui()) {
                return;
            }
            DeviceUtils.setMiuiStatusBarDarkMode(useSystemStatusBarDarkMode, window);
        }
    }

    public static boolean isEnabled() {
        return mIsEnable && Build.VERSION.SDK_INT >= 21;
    }

    public static int getStatusBarHeight(Context context, boolean arg6) {
        int v1 = 0;
        int statusBarHeightResId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (statusBarHeightResId > 0) {
            statusBarHeightResId = arg6
                    ? context.getResources().getDimensionPixelSize(statusBarHeightResId)
                    : ((int) (context.getResources().getDimension(statusBarHeightResId) / context.getResources().getDisplayMetrics().density));
            v1 = statusBarHeightResId;
        }

        statusBarHeightResId = arg6 ? ((int) UIUtils.dp2px(context, 25f)) : 25;
        if (v1 != 0) {
            statusBarHeightResId = v1;
        }

        return statusBarHeightResId;
    }

    public final void setStatusBarResColor(@ColorRes int resColorId) {
        this.mStatusBarColor = AbsApplication.getCtx().getResources().getColor(resColorId);
        this.setStatusBarColor(this.mStatusBarColor);
    }

    private void setUseLightStatusBarInternal(boolean arg2) {
        setUseLightStatusBar(this.mActivity.getWindow(), arg2);
    }

    public final void setup() {
        if (Build.VERSION.SDK_INT >= 21) {
            if (mIsEnable) {
                this.setStatusBarColor(this.mStatusBarColor);
                if (!this.mIsAutoSwitchStatusBarStyle) {
                    this.setUseLightStatusBarInternal(this.mIsUserLightStatusBar);
                }

                if (!this.mFitsSystemWindows) {
                    return;
                }
                this.mActivity.getWindow().getDecorView().setSystemUiVisibility(1024);
            } else {
                this.mActivity.getWindow().clearFlags(-2147483648);
            }
        }
    }

    public final void b(int arg7) {
        int v2 = 2;
        if (Build.VERSION.SDK_INT >= 21 && (mIsEnable)) {
            if (this.mStatusBarAnimator == null) {
                Window v0 = this.mActivity.getWindow();
                int[] v2_1 = new int[v2];
                v2_1[0] = this.mActivity.getWindow().getStatusBarColor();
                v2_1[1] = this.mActivity.getResources().getColor(arg7);
                this.mStatusBarAnimator = ObjectAnimator.ofArgb(v0, "statusBarColor", v2_1);
            } else {
                if (this.mStatusBarAnimator.isStarted()) {
                    this.mStatusBarAnimator.cancel();
                }

                ObjectAnimator v0_1 = this.mStatusBarAnimator;
                int[] v1 = new int[v2];
                v1[0] = this.mActivity.getWindow().getStatusBarColor();
                v1[1] = this.mActivity.getResources().getColor(arg7);
                v0_1.setIntValues(v1);
            }

            this.mStatusBarAnimator.setDuration(300);
            this.mStatusBarAnimator.start();
            this.mStatusBarColor = arg7;
            this.setUseLightStatusBarByColor();
        }
    }

    public final int c() {
        int v0;
        if (this.f != 0) {
            v0 = this.f;
        } else {
            this.f = getStatusBarHeight(this.mActivity, true);
            v0 = this.f;
        }

        return v0;
    }

    private void setStatusBarColor(@ColorInt int statusBarColor) {
        if (Build.VERSION.SDK_INT >= 21 && (mIsEnable)) {
            this.mStatusBarColor = statusBarColor;
            this.setUseLightStatusBarByColor();
            this.mActivity.getWindow().setStatusBarColor(statusBarColor);
        }
    }

    private void setUseLightStatusBarByColor() {
        if (this.mIsAutoSwitchStatusBarStyle) {
            if (this.mIsNightMode) {
                this.setUseLightStatusBarInternal(false);
            } else {
                if (this.mStatusBarColor != R.color.status_bar_color_black
                        && this.mStatusBarColor != R.color.status_bar_color_gallery
                        && this.mStatusBarColor != R.color.status_bar_color_red
                        && this.mStatusBarColor != R.color.status_bar_color_transparent) {
                    if (this.mStatusBarColor == R.color.status_bar_color_detail) {
                        this.setUseLightStatusBarInternal(true);
                    } else {
                    }
                    return;
                }

                this.setUseLightStatusBarInternal(false);
            }
        }
    }

    public interface IStatusBarConfig {
        boolean isEnableImmersedStatusBar();
    }

    public static final class ImmersedStatusBarConfig {
        public int mStatusBarColor;
        public boolean mFitsSystemWindows;
        public boolean c;
        public boolean mIsUseLightStatusBar;
        public boolean mIsAutoSwitchStatusBarStyle;
        public boolean f;

        public ImmersedStatusBarConfig() {
            this.mStatusBarColor = AbsApplication.getCtx().getResources().getColor(com.ss.android.article.browser.R.color.status_bar_color_detail);
            this.mFitsSystemWindows = false;
            this.c = false;
            this.mIsUseLightStatusBar = false;
            this.mIsAutoSwitchStatusBarStyle = false;
            this.f = true;
        }

        public final ImmersedStatusBarConfig setStatusBarColor(@ColorRes int arg2) {
            this.mStatusBarColor = AbsApplication.getCtx().getResources().getColor(arg2);
            return this;
        }
    }
}

